package com.tensquare.article.service;

import com.tensquare.article.dao.ArticleDao;
import com.tensquare.article.dao.CommentDao;
import com.tensquare.entity.Article;
import com.tensquare.entity.Comment;
import com.tensquare.entity.Spit;
import com.tensquare.util.IdWork;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class ArticleService {

    private static String URL = "http://localhost:9005/spit/findById/{id}";

    @Autowired
    ArticleDao articleDao;

    @Autowired
    CommentDao commentDao;

    @Autowired
    IdWork idWork;

    @Autowired
    RestTemplate restTemplate;

    /**
     * 文章审核
     */
    @Transactional
    public void examine(String id){
        Spit spit = restTemplate.getForObject(URL,  Spit.class, id);
        System.err.println(spit);

        Article article = articleDao.findById(id);
        article.setState("1");
        articleDao.update(article);
    }

    /**
     * 文章点赞
     * @param id
     */
    public void updateThumbup( String id){
        articleDao.updateThumbup(id);
    }

    /**
     * 文章评论
     * @param comment
     */
    public void addComment(Comment comment){
            comment.setId(idWork.nextId());
            commentDao.add(comment);
    }

    /**
     * 根据文章id查询所有评论
     * @param articleid
     * @return
     */
    public List<Comment> findByArticleid(String articleid){
        return commentDao.findByArticleid(articleid);
    }

    /**
     * 删除评论
     * @param id
     */
    public void deleteComment(String id){
        commentDao.delete(id);
    }
}
